package agents;
import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/

@SuppressWarnings("serial")
public class MainFrame extends JFrame implements ListSelectionListener {
	private JMenuBar _jMenuBar;
	private JList list;
	private JMenuItem jMenuItem5;
	private JMenuItem jMenuItem4;
	private JMenuItem jMenuItem3;
	private JMenuItem jMenuItem2;
	private JMenuItem jMenuItem1;
	private JMenu jMenu2;
	private JMenu jMenu1;
	private DefaultListModel listModel;
	private ManagementAgent ma;

	public MainFrame(ManagementAgent ma) {
		super("Quick Match Agents");
	    setSize(300,300);
	    setLocation(300,300);
	    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    setVisible(true);
	    this.ma = ma;
	    
	    {
	    	listModel = new DefaultListModel();
	        listModel.addElement("Manager Agent");
	    	
	        list = new JList(listModel);
	    	
	    	list = new JList(listModel);
	        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	        list.setSelectedIndex(0);
	        list.addListSelectionListener(this);
	        list.setVisibleRowCount(5);
	        JScrollPane listScrollPane = new JScrollPane(list);
	    	
	    	getContentPane().add(listScrollPane, BorderLayout.CENTER);
	    }
	    {
	    	_jMenuBar = new JMenuBar();
	    	setJMenuBar(_jMenuBar);
	    	{
	    		jMenu1 = new JMenu();
	    		_jMenuBar.add(jMenu1);
	    		jMenu1.setText("Start");
	    		jMenu1.setFont(new java.awt.Font("Helvetica",0,14));
	    		{
	    			jMenuItem1 = new JMenuItem();
	    			jMenu1.add(jMenuItem1);
	    			jMenuItem1.setText("Strike Agent");
	    			jMenuItem1.addActionListener(new ActionListener() {
	    				public void actionPerformed(ActionEvent evt) {
	    					jMenuItem1ActionPerformed(evt);
	    				}
	    			});
	    		}
	    		{
	    			jMenuItem2 = new JMenuItem();
	    			jMenu1.add(jMenuItem2);
	    			jMenuItem2.setText("Perfect Agent");
	    			jMenuItem2.addActionListener(new ActionListener() {
	    				public void actionPerformed(ActionEvent evt) {
	    					jMenuItem2ActionPerformed(evt);
	    				}
	    			});
	    		}
	    		{
	    			jMenuItem3 = new JMenuItem();
	    			jMenu1.add(jMenuItem3);
	    			jMenuItem3.setText("Loser Agent");
	    			jMenuItem3.addActionListener(new ActionListener() {
	    				public void actionPerformed(ActionEvent evt) {
	    					jMenuItem3ActionPerformed(evt);
	    				}
	    			});
	    		}
	    	}
	    	{
	    		jMenu2 = new JMenu();
	    		_jMenuBar.add(jMenu2);
	    		jMenu2.setText("Kill");
	    		jMenu2.setFont(new java.awt.Font("Helvetica",0,14));
	    		{
	    			jMenuItem4 = new JMenuItem();
	    			jMenu2.add(jMenuItem4);
	    			jMenuItem4.setText("All Agents");
	    			jMenuItem4.addActionListener(new ActionListener() {
	    				public void actionPerformed(ActionEvent evt) {
	    					jMenuItem4ActionPerformed(evt);
	    				}
	    			});
	    		}
	    		{
	    			jMenuItem5 = new JMenuItem();
	    			jMenu2.add(jMenuItem5);
	    			jMenuItem5.setText("Agent");
	    			jMenuItem5.addActionListener(new ActionListener() {
	    				public void actionPerformed(ActionEvent evt) {
	    					jMenuItem5ActionPerformed(evt);
	    				}
	    			});
	    		}
	    	}
	    }
	}
	
	// Startet Strike wenn noch keiner gestartet ist
	private void jMenuItem1ActionPerformed(ActionEvent evt) {
		//Strike Agent already exists
	    if (alreadyInList("Strike Agent")) {
	        Toolkit.getDefaultToolkit().beep();
	        return;
	    }
	    
	    listModel.addElement("Strike Agent");
	    
	    ma.addAgent("strikemaster");
	}

	// Startet neuen Perfectagent
	private void jMenuItem2ActionPerformed(ActionEvent evt) {
        String s = ma.addAgent("perfect");
        
        listModel.addElement(s);
	}
	
	// Startet neuen loseragent
	private void jMenuItem3ActionPerformed(ActionEvent evt) {
		String s = ma.addAgent("loser");
	    
		listModel.addElement(s);
	}

	// Loecht alle Agenten
	private void jMenuItem4ActionPerformed(ActionEvent evt) {
	    for (int i = 0; i < listModel.size(); i++) {
	    	removeAgent(listModel.get(i).toString());
	    }
	    
	    listModel.removeAllElements();
	}

	// Loescht den ausgewaehlten Agenten
	private void jMenuItem5ActionPerformed(ActionEvent evt) {
		int index = list.getSelectedIndex();
		
		if (index != -1) {
			removeAgent(listModel.get(index).toString());
			listModel.remove(index);
				
			int size = listModel.getSize();
		
		    if (size > 0) { //Nobody's left, disable firing.
		        if (index == listModel.getSize()) {
		        	//removed item in last position
		            index--;
		        }
		        list.setSelectedIndex(index);
		        list.ensureIndexIsVisible(index);
		   }
		} else {
			Toolkit.getDefaultToolkit().beep();
		}
	}

	// Ueberprueft ob der Agent schon existiert
	private boolean alreadyInList(String string) {
		return listModel.contains(string);
	}
	
	// Entfernt einen Agenten mit einem gewissen String
	private void removeAgent(String string) {
		if (string.contains("Strike Agent")) {
			ma.removeAgent("strikemaster");
		} else if (string.contains("Manager Agent")) {
			ma.removeAgent("manager");
		} else {
			ma.removeAgent(string);
		}
	}

	@Override
	public void valueChanged(ListSelectionEvent arg0) {
		// Wenn man auf ein Item in der List klickt
	}
}
